### Load libraries 

{install.packages("survey")
  install.packages("srvyr")
  install.packages("ggplot2")
  install.packages("dplyr")
  install.packages("data.table")
  install.packages("questionr")
  install.packages("ggthemes")
  install.packages("ggpubr")
  install.packages("moments")
  install.packages("ggrepel")
}

{library(data.table)
  library(survey)
  library(srvyr)
  library(dplyr)
  library(data.table)
  library(questionr)
  library(ggthemes)
  library(ggplot2)
  library(ggpubr)
  library(moments)
  library(ggrepel)
}

### Loading data from csv

PBS_data <- read.csv2("PBS_zbiór.csv", sep = ",")

PBS_data <- as.data.table(PBS_data)

PBS_data$waga <- as.numeric(PBS_data$waga)

### Create survey design 

### Survey design for survey

srs_design <- svydesign(id=~nr_wyw, weights=~waga, data=PBS_data)

### Treatment

srs_treatment <- svydesign(id=~nr_wyw, weights=~waga, data=PBS_data[wariant == 1])

### Control 

srs_control <- svydesign(id=~nr_wyw, weights=~waga, data=PBS_data[wariant == 0])


### Recode variable for cuts in public spending (treatment group)

PBS_data[, I5_1 := fifelse(I5_1 == "Wskazano", 1, 0)]

PBS_data[, I5_2 := fifelse(I5_2 == "Wskazano", 1, 0)]

PBS_data[, I5_3 := fifelse(I5_3 == "Wskazano", 1, 0)]

PBS_data[, I5_4 := fifelse(I5_4 == "Wskazano", 1, 0)]

PBS_data[, I5_5 := fifelse(I5_5 == "Wskazano", 1, 0)]

PBS_data[, I5_6 := fifelse(I5_6 == "Wskazano", 1, 0)]

PBS_data[, I5_7 := fifelse(I5_7 == "Wskazano", 1, 0)]

PBS_data[, I5_8 := fifelse(I5_8 == "Wskazano", 1, 0)]

### Recode variable for hikes in public spending (control)

PBS_data[, I6_1 := fifelse(I6_1 == "Wskazano", 1, 0)]

PBS_data[, I6_2 := fifelse(I6_2 == "Wskazano", 1, 0)]

PBS_data[, I6_3 := fifelse(I6_3 == "Wskazano", 1, 0)]

PBS_data[, I6_4 := fifelse(I6_4 == "Wskazano", 1, 0)]

PBS_data[, I6_5 := fifelse(I6_5 == "Wskazano", 1, 0)]

PBS_data[, I6_6 := fifelse(I6_6 == "Wskazano", 1, 0)]

PBS_data[, I6_7 := fifelse(I6_7 == "Wskazano", 1, 0)]

PBS_data[, I6_8 := fifelse(I6_8 == "Wskazano", 1, 0)]

### Recode variable for cuts in public spending (treatment group)

PBS_data[, P11_1 := fifelse(P11_1 == "Wskazano", 1, 0)]

PBS_data[, P11_2 := fifelse(P11_2 == "Wskazano", 1, 0)]

PBS_data[, P11_3 := fifelse(P11_3 == "Wskazano", 1, 0)]

PBS_data[, P11_4 := fifelse(P11_4 == "Wskazano", 1, 0)]

PBS_data[, P11_5 := fifelse(P11_5 == "Wskazano", 1, 0)]

PBS_data[, P11_6 := fifelse(P11_6 == "Wskazano", 1, 0)]

PBS_data[, P11_7 := fifelse(P11_7 == "Wskazano", 1, 0)]

PBS_data[, P11_8 := fifelse(P11_8 == "Wskazano", 1, 0)]

### Recode variable for hikes in public spending (treatment group)

PBS_data[, P12_1 := fifelse(P12_1 == "Wskazano", 1, 0)]

PBS_data[, P12_2 := fifelse(P12_2 == "Wskazano", 1, 0)]

PBS_data[, P12_3 := fifelse(P12_3 == "Wskazano", 1, 0)]

PBS_data[, P12_4 := fifelse(P12_4 == "Wskazano", 1, 0)]

PBS_data[, P12_5 := fifelse(P12_5 == "Wskazano", 1, 0)]

PBS_data[, P12_6 := fifelse(P12_6 == "Wskazano", 1, 0)]

PBS_data[, P12_7 := fifelse(P12_7 == "Wskazano", 1, 0)]

PBS_data[, P12_8 := fifelse(P12_8 == "Wskazano", 1, 0)]

### Treatment status recoding

### Cuts

PBS_data[, I5_P11_1 := fifelse(is.na(I5_1), PBS_data$P11_1, PBS_data$I5_1)]

PBS_data[, I5_P11_2 := fifelse(is.na(I5_2), PBS_data$P11_2, PBS_data$I5_2)]

PBS_data[, I5_P11_3 := fifelse(is.na(I5_3), PBS_data$P11_3, PBS_data$I5_3)]

PBS_data[, I5_P11_4 := fifelse(is.na(I5_4), PBS_data$P11_4 , PBS_data$I5_4)]

PBS_data[, I5_P11_5 := fifelse(is.na(I5_5), PBS_data$P11_5, PBS_data$I5_5)]

PBS_data[, I5_P11_6 := fifelse(is.na(I5_6), PBS_data$P11_6, PBS_data$I5_6)]

PBS_data[, I5_P11_7 := fifelse(is.na(I5_7), PBS_data$P11_7, PBS_data$I5_7)]

PBS_data[, I5_P11_8 := fifelse(is.na(I5_8), PBS_data$P11_8, PBS_data$I5_8)]

### Hikes

PBS_data[, I6_P12_1 := fifelse(is.na(I6_1), PBS_data$P12_1, PBS_data$I6_1)]

PBS_data[, I6_P12_2 := fifelse(is.na(I6_2), PBS_data$P12_2, PBS_data$I6_2)]

PBS_data[, I6_P12_3 := fifelse(is.na(I6_3), PBS_data$P12_3, PBS_data$I6_3)]

PBS_data[, I6_P12_4 := fifelse(is.na(I6_4), PBS_data$P12_4, PBS_data$I6_4)]

PBS_data[, I6_P12_5 := fifelse(is.na(I6_5), PBS_data$P12_5, PBS_data$I6_5)]

PBS_data[, I6_P12_6 := fifelse(is.na(I6_6), PBS_data$P12_6, PBS_data$I6_6)]

PBS_data[, I6_P12_7 := fifelse(is.na(I6_7), PBS_data$P12_7, PBS_data$I6_7)]

PBS_data[, I6_P12_8 := fifelse(is.na(I6_8), PBS_data$P12_8, PBS_data$I6_8)]

### Treatment status 

PBS_data[, wariant := fcase (wariant == "Z interwencja", "Treatment",
                             wariant == "Bez interwencji", "Control")]

### Creating variables 

### Sex 

PBS_data[, M1 := ifelse(M1 == "Mezczyzna", 1, 0)]

### Age

PBS_data[, age := 2021 - Rok_urodzenia]

### Level of age

PBS_data[, M2_rekod := fcase(M2 == "18-24", 1,
                             M2 == "25-34", 2,
                             M2 == "35-44", 3,
                             M2 == "45-54", 4,
                             M2 == "55+", 5
)]


### Education level

dsub <- subset(srs_design, M1 == 0)

svyby(~I5_7, ~wariant, dsub, svymean, na = TRUE)

svyby(~M1, ~I5_7, srs_control, svymean, na = TRUE)

PBS_data[, M3_rekod := fcase(M3_rekod == "Niskie", 1,
                             M3_rekod == "Srednie", 2,
                             M3_rekod == "Wyzsze\xa0", 3
)]
### Age 

PBS_data[, age := 2021 - Rok_urodzenia]

### Size of place of residence

PBS_data[, M5 := fcase (M5 == "Wies", 1,
                        M5 == "Male miasto (ponizej 20 tys. mieszkanc\xf3w)", 2,
                        M5 == "Srednie miasto (100-500 tys. mieszkanc\xf3w)", 3,
                        M5 == "Miasto (20-100 tys. mieszkanc\xf3w)", 4,
                        M5 == "Duze miasto (powyzej 500 tys. mieszkanc\xf3w)", 5)]

### Economic literacy 

PBS_data[, M6 := fcase (M6 == "Duza", 5,
                        M6 == "Raczej duza", 4,
                        M6 == "Srednia", 3,
                        M6 == "Raczej mala", 2,
                        M6 == "Mala", 1)]

### Size of household 

PBS_data[, .N, keyby = .(M13)]

### Level of individual income 

PBS_data[, M15 := fcase (M15 == "Do 1000 zl", 1,
                         M15 == "Od 1001 do 2000 zl", 2,
                         M15 == "Od 2001 do 3000 zl", 3,
                         M15 == "Od 3001 do 4000 zl", 4,
                         M15 == "Od 4001 do 5000 zl", 5,
                         M15 == "Od 5001 do 6000 zl ", 6,
                         M15 == "Od 6001 do 8000 zl", 7,
                         M15 == "Powyzej 8000 zl", 8
)]

### Level of household income

PBS_data[, M16 := fcase (M16 == "Do 1000 zl", 1,
                         M16 == "Od 1001 do 2000 zl", 2,
                         M16 == "Od 2001 do 3000 zl", 3,
                         M16 == "Od 3001 do 4000 zl", 4,
                         M16 == "Od 4001 do 5000 zl", 5,
                         M16 == "Od 5001 do 6000 zl ", 6,
                         M16 == "Od 6001 do 9000 zl", 7,
                         M16 == "Od 9001 do 12000 zl", 8,
                         M16 == "Powyzej 12000 zl", 9
)]

### Level of household income (per person)

PBS_data[, M16_p := M16/M13]

### Participation in national elections

PBS_data[, M17 := fcase(M17 == "Tak", 1, 
                        M17 == "Nie", 0,
                        M17 == "Trudno powiedziec", 0)]
### Support for government

PBS_data[, M_20 := fifelse(M18 == "Prawo i Sprawiedliwosc", 1, 
                           0)]

### Activity status


PBS_data[, M11 := fcase (M11_1 == "Wskazano", "Work",
                         M11_2 == "Wskazano", "Maternity or parental leave",
                         M11_3 == "Wskazano" | M11_4 == "Wskazano", "Professional inactivity",
                         M11_5 == "Wskazano", "In education",
                         M11_6 == "Wskazano", "Pension"
)]

### Binary for not working

PBS_data[, M11_3_b := ifelse(M11 == "Professional inactivity", 1, 0)]

### Binary for student

PBS_data[, M11_5_b := ifelse(M11 == "In education", 1, 0)]

### Binary for employment

PBS_data[, M11_1_b := ifelse(M11 == "Work", 1, 0)]

### Type of employment 

PBS_data[, M12 := fcase (M12_1 == "Wskazano", "Contract work",
                         M12_2 == "Wskazano", "Self-employment",
                         M12_3 == "Wskazano", "Agricultural business"
)]

### Binary for self-employed 

PBS_data[, M12_2_b := ifelse(M12 == "Self-employment", 1, 0)]

### Binary for pension

PBS_data[, M11_6_b := ifelse(M11 == "Pension", 1, 0)]

### Binary for parental or maternity leave

PBS_data[, M11_2_b := ifelse(M11 == "Maternity or parental leave", 1, 0)]

### Binary for young

PBS_data[, young := ifelse(M2 == "18-24", 1, 0)]

### Binary for high economic literacy

PBS_data[, highknowledge :=  ifelse(M6 == 5 | M6 == 4 , 1, 0)]

### Binary for higher education

PBS_data[, higheredu :=  ifelse(M3 == "Wyzsze", 1, 0)]

### Binary for city

PBS_data[, city := ifelse(M5 > 1, 1, 0)]

### Binary for high-income

PBS_data[, highincome := ifelse(M15 > 6, 1, 0)]           

### Defining survey design 

srs_design <- svydesign(id=~nr_wyw, weights=~waga, data=PBS_data)

### Treatment

srs_treatment <- svydesign(id=~nr_wyw, weights=~waga, data=PBS_data[wariant == "Treatment"])

### Control 

srs_control <- svydesign(id=~nr_wyw, weights=~waga, data=PBS_data[wariant == "Control"])

